package com.hpjpw.idoapp.controller;

/**
 * @BelongsProject: ruoyi
 * @BelongsPackage: com.hpjpw.ido.controller
 * @Author: hpj
 * @CreateTime: 2021-01-02 15:52
 * @Description: 文件工具
 */

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * 登录验证
 *
 * @author hpj
 */
@Api(value = "文件上传")
@RestController
@RequestMapping("/ido/file")
public class IDoFileController {
    @Autowired
    private ISysUserService userService;
    @Autowired
    private TokenService tokenService;

    /**
     * 文件上传
     */
    @ApiOperation("文件上传")
    @PostMapping("/upload")
    public AjaxResult avatar(@RequestParam("file") MultipartFile file) throws IOException {
        if (!file.isEmpty()) {
            LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
            String filePath = FileUploadUtils.upload(RuoYiConfig.getProfile() + "/userUpload/" + loginUser.getUser().getUserName(), file);

            AjaxResult ajax = AjaxResult.success();
            ajax.put("fileUrl", filePath);
            tokenService.setLoginUser(loginUser);
            return ajax;
        }
        return AjaxResult.error("上传图片异常，请联系管理员");
    }
}
